package com.ziq.zcamp.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;

public class RadiusImageView extends AppCompatImageView {

    public RadiusImageView(Context context) {
        this(context, null, 0);
    }

    public RadiusImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadiusImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
    }

    public void setImageBitmapRound(Bitmap bm,int radius,boolean isTop){
        if (isTop){
            setImageBitmap(roundTopBitmapByShader(bm,bm.getWidth(),bm.getHeight(),radius));
        }else {
            setImageBitmap(roundBottomBitmapByShader(bm,bm.getWidth(),bm.getHeight(),radius));
        }
    }

    /**
     * 利用BitmapShader绘制底部圆角图片
     * @param bitmap 待处理图片
     * @param outWidth 结果图片宽度，一般为控件的宽度
     * @param outHeight 结果图片高度，一般为控件的高度
     * @param radius 圆角半径大小
     * @return 结果图片
     */
    private Bitmap roundBottomBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius) {
        if(bitmap == null) {
            throw new NullPointerException("Bitmap can't be null");
        }
        // 初始化缩放比
        float widthScale = outWidth * 1.0f / bitmap.getWidth();
        float heightScale = outHeight * 1.0f / bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.setScale(widthScale, heightScale);
        // 初始化绘制纹理图
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        // 根据控件大小对纹理图进行拉伸缩放处理
        bitmapShader.setLocalMatrix(matrix);
        // 初始化目标bitmap
        Bitmap targetBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        // 初始化目标画布
        Canvas targetCanvas = new Canvas(targetBitmap);
        // 初始化画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(bitmapShader);
        // 利用画笔绘制底部圆角
        targetCanvas.drawRoundRect(new RectF(0, outHeight - 2 * radius, outWidth, outHeight), radius, radius, paint);
        // 利用画笔绘制顶部上面直角部分
        targetCanvas.drawRect(new RectF(0, 0, outWidth, outHeight - radius), paint);
        return targetBitmap;
    }

    /**
     * 利用BitmapShader绘制底部圆角图片
     * @param bitmap 待处理图片
     * @param outWidth 结果图片宽度，一般为控件的宽度
     * @param outHeight 结果图片高度，一般为控件的高度
     * @param radius 圆角半径大小
     * @return 结果图片
     */
    private Bitmap roundTopBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius) {
        if(bitmap == null) {
            throw new NullPointerException("Bitmap can't be null");
        }
        // 初始化缩放比
        float widthScale = outWidth * 1.0f / bitmap.getWidth();
        float heightScale = outHeight * 1.0f / bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.setScale(widthScale, heightScale);
        // 初始化绘制纹理图
        BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        // 根据控件大小对纹理图进行拉伸缩放处理
        bitmapShader.setLocalMatrix(matrix);
        // 初始化目标bitmap
        Bitmap targetBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        // 初始化目标画布
        Canvas targetCanvas = new Canvas(targetBitmap);
        // 初始化画笔
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setShader(bitmapShader);
        // 利用画笔绘制顶部圆角
        targetCanvas.drawRoundRect(new RectF(0, 0, outWidth, 2 * radius), radius, radius, paint);
        // 利用画笔绘制底部直角部分
        targetCanvas.drawRect(new RectF(0, radius, outWidth, outHeight), paint);
        return targetBitmap;
    }
}